package hyj.qx.kuaican.servlet;

import com.alibaba.fastjson.JSONObject;
import hyj.qx.kuaican.exception.BusinessException;
import hyj.qx.kuaican.model.CyOrder;
import hyj.qx.kuaican.model.ErrorCode;
import hyj.qx.kuaican.model.Item;
import hyj.qx.kuaican.model.Order;
import hyj.qx.kuaican.service.OrderRepository;
import hyj.qx.kuaican.service.impl.MemoryOrderRepository;
import hyj.qx.kuaican.service.impl.OrderService;
import hyj.qx.kuaican.util.RequestUtil;
import hyj.qx.kuaican.util.ResponseUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

@WebServlet(name = "OrderServlet", urlPatterns = "/order/*")
public class OrderServlet extends RouteServlet {
    OrderService orderService = new OrderService();
    OrderRepository orderRepository = new MemoryOrderRepository();

    @Override
    protected Class getMyClass() {
        return this.getClass();
    }

    public void createOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {

        JSONObject param = RequestUtil.getParam(request);
        List<Item> items = param.getJSONArray("items").toJavaList(Item.class);
        boolean VIP = param.getBoolean("VIP");

        try {
            Order order = orderService.createOrder(items, VIP);
            JSONObject yes = new JSONObject();
            yes.put("Order", order);
            ResponseUtil.success(response, yes);
        } catch (BusinessException e) {
            ResponseUtil.fail(response, e.getErrorCode());
        }
    }

    public void cancelOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        JSONObject param = RequestUtil.getFormData(request);
        try {
            orderService.cancelOrder(param.getString("OrderId"));
            ResponseUtil.success(response, null);
        } catch (BusinessException E) {
            ResponseUtil.fail(response, E.getErrorCode());
        }
    }

    public void getOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        JSONObject param = RequestUtil.getFormData(request);
        JSONObject yes = new JSONObject();
        try {
            Order order = orderService.getOrder(param.getString("OrderId"));
            yes.put("Order", order);
            ResponseUtil.success(response, yes);
        } catch (BusinessException e) {
            ResponseUtil.fail(response, e.getErrorCode());
        }
    }

    public void UpdateOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        JSONObject param = RequestUtil.getParam(request);
        String orderId = param.getString("OrderId");
        List<Item> allItems = param.getJSONArray("allItems").toJavaList(Item.class);
        List<Item> addItems = param.getJSONArray("addItems").toJavaList(Item.class);
        List<Item> reItems = param.getJSONArray("reItems").toJavaList(Item.class);
        boolean VIP = param.getBoolean("VIP");
        try {
            Order order = orderService.updateOrder(orderId, allItems, addItems, reItems, VIP);

            ResponseUtil.success(response, null);
        } catch (BusinessException e) {
            ResponseUtil.fail(response, e.getErrorCode());
        }
    }


    public void ShengHe(HttpServletRequest request, HttpServletResponse response) throws IOException {
        JSONObject param = RequestUtil.getFormData(request);
        String orderId = param.getString("OrderId");
        String Type = param.getString("Type");
        JSONObject yes = new JSONObject();
        Order order = orderRepository.findUpOrderById(orderId);
        if (Type.equals("ok")) {
            yes.put("Order", order);
            orderRepository.removeOrder(orderId);//移除老订单
            ResponseUtil.success(response, yes);//同意则把新订单返回
        } else {
            ResponseUtil.fail(response, ErrorCode.NOT_OK);
        }
    }

    public void findUpOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        JSONObject yes = new JSONObject();
        try {
            List<CyOrder> CyOrders = orderService.getCyOrder();
            yes.put("CyOrder", CyOrders);
            ResponseUtil.success(response, yes);
        } catch (BusinessException e) {
            ResponseUtil.fail(response, e.getErrorCode());
        }
    }

}
